package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.JwTeachingMaterialsInfoBo;
import org.dromara.system.domain.vo.JwTeachingMaterialsInfoVo;
import org.dromara.system.domain.JwTeachingMaterialsInfo;
import org.dromara.system.mapper.JwTeachingMaterialsInfoMapper;
import org.dromara.system.service.IJwTeachingMaterialsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 教材信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-26
 */
@RequiredArgsConstructor
@Service
public class JwTeachingMaterialsInfoServiceImpl implements IJwTeachingMaterialsInfoService {

    private final JwTeachingMaterialsInfoMapper baseMapper;

    /**
     * 查询教材信息
     */
    @Override
    public JwTeachingMaterialsInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询教材信息列表
     */
    @Override
    public TableDataInfo<JwTeachingMaterialsInfoVo> queryPageList(JwTeachingMaterialsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JwTeachingMaterialsInfo> lqw = buildQueryWrapper(bo);
        Page<JwTeachingMaterialsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询教材信息列表
     */
    @Override
    public List<JwTeachingMaterialsInfoVo> queryList(JwTeachingMaterialsInfoBo bo) {
        LambdaQueryWrapper<JwTeachingMaterialsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JwTeachingMaterialsInfo> buildQueryWrapper(JwTeachingMaterialsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JwTeachingMaterialsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTmId()), JwTeachingMaterialsInfo::getTmId, bo.getTmId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), JwTeachingMaterialsInfo::getName, bo.getName());
        lqw.eq(bo.getPrice() != null, JwTeachingMaterialsInfo::getPrice, bo.getPrice());
        lqw.eq(bo.getNum() != null, JwTeachingMaterialsInfo::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), JwTeachingMaterialsInfo::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增教材信息
     */
    @Override
    public Boolean insertByBo(JwTeachingMaterialsInfoBo bo) {
        JwTeachingMaterialsInfo add = MapstructUtils.convert(bo, JwTeachingMaterialsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改教材信息
     */
    @Override
    public Boolean updateByBo(JwTeachingMaterialsInfoBo bo) {
        JwTeachingMaterialsInfo update = MapstructUtils.convert(bo, JwTeachingMaterialsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JwTeachingMaterialsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除教材信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
